#include <os/mm.h>

ptr_t memCurr = FREEMEM;
mem_avaliable_t mem_mangement[100]; 

ptr_t allocPage(int numPage)
{
    // align PAGE_SIZE
    ptr_t ret = ROUND(memCurr, PAGE_SIZE);
    memCurr = ret + numPage * PAGE_SIZE;
    return memCurr;
}

void* kmalloc(size_t size)
{
    ptr_t ret = ROUND(memCurr, 4);
    memCurr = ret + size;
    return (void*)ret;
}
ptr_t my_allocPage(ptr_t baseAddr,int numPage){//从baseaddr开始分配
	baseAddr = ROUND(baseAddr, PAGE_SIZE);
	int i;
	for(i=0;i<numPage;i++){
		if(mem_mangement[(baseAddr-FREEMEM)/PAGE_SIZE+i]==MEM_USED){
			printk("has allocated %d pages and stop\n",i);
			return baseAddr+i*PAGE_SIZE;
		}
		mem_mangement[(baseAddr-FREEMEM)/PAGE_SIZE+i]=MEM_USED;
	}
	return baseAddr+i*PAGE_SIZE;//返回值是栈顶
}
void freePage(ptr_t baseAddr, int numPage){//baseaddr是栈顶
	baseAddr = ROUND(baseAddr, PAGE_SIZE);
	int i;
	for(i=0;i<numPage;i++)
		mem_mangement[(baseAddr-FREEMEM)/PAGE_SIZE-1-i]=MEM_FREE;
}